'use strict'
const crypto = require('crypto');

//生成离目标节点较近的id
function genNeighborId(target, nid) {
    return Buffer.concat([target.slice(0,15),nid.slice(15)]);
}

//生成随机nid
function randomId() {
    return crypto.createHash('sha1').update(crypto.randomBytes(20)).digest();
}

//节点为26位
function decodeNodes(nodes) {
    let arr = [];
    const len = nodes.length;
    if (len % 26 != 0){
        return arr;
    }

    for (let i = 0; i+26 <= nodes.length; i+=26){
        arr.push({
            nid:nodes.slice(i,i+20),
            address:nodes[i+20] + '.' + nodes[i + 21] + '.' +
            nodes[i + 22] + '.' + nodes[i + 23],
            port:nodes.readUInt16BE(i+24)
        });
    }
    // console.log('decodeNodes arr-->',arr);
    return arr;
}

module.exports = {
    randomId,
    decodeNodes,
    genNeighborId
}